//
//  QWriteDataVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/12/20.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QWriteDataVC.h"
#import "QDTSuccessVC.h"
@interface QWriteDataVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UITextField *idTextField;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UIButton *tButton;

@end

@implementation QWriteDataVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"填写资料";
    _tButton.layer.cornerRadius = 7;
    [QViewUtility setupNavigationBarBackForController:self backSelector:@selector(writeDataback)];
    _nameTextField.tag = 1;
    _nameTextField.delegate = self;
    
    _idTextField.tag = 2;
    _idTextField.delegate = self;
    
    _phoneTextField.tag = 3;
    _phoneTextField.delegate = self;
    
   }


- (void)writeDataback{

    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)buttonAction:(id)sender {
    NSString *idStr = [_idTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    if(_nameTextField.text.length == 0){
    
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入正确姓名" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];

    }else if (idStr.length != 18 && idStr.length != 15){
    
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入正确身份证号" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];

    }else if (_phoneTextField.text.length != 11){
    
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入正确手机号" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];

    }else{
    
        [self requestData:idStr];
    }
    
}

- (void)requestData:(NSString *)strid{

    NSString *url = [QParamService urlWithAction:@"applyForMarketing"];
    NSDictionary *param = [QParamService reviewpleaseWithtrueName:_nameTextField.text idcard:strid tel:_phoneTextField.text];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
             [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSignup object:nil];
            QDTSuccessVC *vc = [[QDTSuccessVC alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];


}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if(textField.tag == 2){
    
        NSString *text = [textField text];
        
        NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\b"];
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
            return NO;
        }
        
        text = [text stringByReplacingCharactersInRange:range withString:string];
        text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        NSString *newString = @"";
        while (text.length > 0) {
            NSString *subString = [text substringToIndex:MIN(text.length, 6)];
            newString = [newString stringByAppendingString:subString];
            if (subString.length == 6) {
                newString = [newString stringByAppendingString:@" "];
            }
            text = [text substringFromIndex:MIN(text.length, 6)];
        }
        
        newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
        
        if (newString.length >= 21) {
            return NO;
        }
        
        [textField setText:newString];
        return NO;

       
    }else{
    
        return YES;
    }
    
}




@end
